package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.item.pojo.Item;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //http://localhost:10010/item/list?page=1&size=5
    @GetMapping("/list")
    public PageDTO<Item> findByPage(@RequestParam("page") int page,
                                    @RequestParam("size") int size) {
        Page<Item> itemPage = new Page<>(page, size);
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        int count = itemService.count(wrapper);
        Page<Item> pageInfo = itemService.page(itemPage, wrapper);
        List<Item> records = pageInfo.getRecords();
        PageDTO<Item> itemPageDTO = new PageDTO<>();
        itemPageDTO.setTotal((long) count);
        itemPageDTO.setList(records);
        return itemPageDTO;
    }

    //    /item/{id}
    @GetMapping("/{id}")
    public Item findByID(@PathVariable("id") Long id) {
        return itemService.getById(id);
    }

    @PostMapping
    public void save(@RequestBody Item item) {
        item.setStatus(1);
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        itemService.save(item);
        rabbitTemplate.convertAndSend("mall.topic", "mall.insert", item.getId());
    }

    //    http://localhost:10010/item/status/317578/2
    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        Item item = itemService.getById(id);
        item.setStatus(status);
        itemService.updateById(item);
        if (status == 1){
            rabbitTemplate.convertAndSend("mall.topic", "mall.insert", item.getId());
        }
        if (status == 2) {
            rabbitTemplate.convertAndSend("mall.topic", "mall.delete", item.getId());
        }
    }

    @PutMapping
    public void update(@RequestBody Item item) {
        item.setUpdateTime(new Date());
        itemService.updateById(item);
        rabbitTemplate.convertAndSend("mall.topic", "mall.insert", item.getId());
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable("id") Long id) {
        itemService.removeById(id);
        rabbitTemplate.convertAndSend("mall.topic", "mall.delete", id);
    }

    @PutMapping("/stock/{id}/{num}")
    public void subStock(@PathVariable("id")Long id,@PathVariable("num") Integer num){
        itemService.subStock(id,num);
        rabbitTemplate.convertAndSend("mall.topic", "mall.insert", id);
    }
}
